from concepts.attributes import Attribute, Object, _


class Policy(Object):
    owner   = Attribute()
    entries = Attribute(initial = {}, convert = dict, transform = dict)

    def add(self, entry):
        self._entries[entry.about] = entry
    
    def entry(self, about):
        return self._entries.get(about, None)


class PolicyEntry(Object):
    about  = Attribute()
    label  = Attribute()
    values = Attribute(initial = {}, convert = dict, transform = dict)

    def add(self, value):
        self._values[value.label] = value
    
    def value(self, label):
        return self._values.get(label, None)


class PolicyValue(Object):
    label   = Attribute()
    min     = Attribute()
    max     = Attribute()
    step    = Attribute()
    value   = Attribute(writable = True)
    current = Attribute(initial = 0, writable = True)

    @property
    def can_shrink(self):
        return self.current > self.value

    @property
    def can_grow(self):
        return self.current < self.value
    
    @property
    def number_above(self):
        if not self.can_shrink:
            return 0
        else:
            return self.current - self.value
    
    @property
    def number_below(self):
        if not self.can_grow:
            return 0
        else:
            return self.value - self.current
    
    def increase(self):
        self.value += self.step
        
        if self.value > self.max:
            self.value = self.max
    
    def decrease(self):
        self.value -= self.step
        
        if self.value < self.min:
            self.value = self.min
